<?php

/**
 * This is the model class for table "{{object_order}}".
 *
 * The followings are the available columns in table '{{object_order}}':
 * @property string $object_id
 * @property string $order_id
 * @property string $order_number
 * @property string $object_order_price
 * @property string $object_order_total_price
 */
class ObjectOrder extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ObjectOrder the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{object_order}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('object_id, order_id, order_number, object_order_price, object_order_total_price', 'required'),
            array('object_id, order_id, order_number, object_order_price, object_order_total_price', 'length', 'max' => 20),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('object_id, order_id, order_number, object_order_price, object_order_total_price', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'object_id' => 'Object',
            'order_id' => 'Order',
            'order_number' => 'Order Number',
            'object_order_price' => 'Object Order Price',
            'object_order_total_price' => 'Object Order Total Price',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('object_id', $this->object_id, true);
        $criteria->compare('order_id', $this->order_id, true);
        $criteria->compare('order_number', $this->order_number, true);
        $criteria->compare('object_order_price', $this->object_order_price, true);
        $criteria->compare('object_order_total_price', $this->object_order_total_price, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function getInfoText($obj) {
        $Object = Object::model()->findByPk($obj);

        if ($Object) {
            return '<b>' . $Object->object_name . '</b>';
        } else
            return '';
    }

    public static function getListProduct($orderID) {
        $result = '';
        $lProduct = ObjectOrder::model()->findAll(' t.`order_id`=:id', array(':id' => $orderID));
        $order = Order::model()->find(' t.`order_id`=:id', array(':id' => $orderID));
        $result = "<table cellspacing='0' cellpadding='0' border='0' style='font:11px Verdana,Arial,Helvetica,sans-serif;color:#333; padding: 5px 10px;'>";
        foreach ($lProduct as $lp) {

            $result .= '<tr>
                                   <td style="border-bottom: 1px dotted #ccc;">' . $lp['order_number'] . ' x ' . ObjectOrder::getInfoText($lp['object_id']) . ':</td>
                                   <td style="border-bottom: 1px dotted #ccc;">&nbsp;&nbsp;</td>
                                   <td style="border-bottom: 1px dotted #ccc;" width="150" align="right"> <b>&nbsp;' . number_format($lp['object_order_total_price'], 2) . ' ' . $order['sale_currency'] . '</b></td>
                               </tr>';
        }

        $result .= '<tr><td colspan="2" align="right">Total:</td><td align="right"><b>&nbsp;' . number_format($order['order_total'], 2) . ' ' . $order['sale_currency'] . '</b></td></tr>
                           <tr><td colspan="3">&nbsp;</td></tr>
                           </table>';

        if ($result)
            return $result;
    }

}
